/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.gwc.yy.liuxue.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.gwc.yy.liuxue.entity.Recommend;
import com.gwc.yy.liuxue.entity.School;
import com.gwc.yy.liuxue.service.RecommendService;
import com.gwc.yy.liuxue.service.SchoolService;

/**
 * 推荐记录Controller
 * @author 黎俊涛
 * @version 2017-04-14
 */
@Controller
@RequestMapping(value = "${adminPath}/liuxue/recommend")
public class RecommendController extends BaseController {

	@Autowired
	private RecommendService recommendService;
	@Autowired
	private SchoolService schoolService;
	
	@ModelAttribute
	public Recommend get(@RequestParam(required=false) String id) {
		Recommend entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = recommendService.get(id);
		}
		if (entity == null){
			entity = new Recommend();
		}
		return entity;
	}
	
	@RequiresPermissions("liuxue:recommend:view")
	@RequestMapping(value = {"list", ""})
	public String list(Recommend recommend, HttpServletRequest request, HttpServletResponse response, Model model) {
		recommend.setUser(UserUtils.getUser());
		Page<Recommend> page = recommendService.findPage(new Page<Recommend>(request, response), recommend); 
		model.addAttribute("page", page);
		return "yy/liuxue/recommendList";
	}

	@RequiresPermissions("liuxue:recommend:view")
	@RequestMapping(value = "form")
	public String form(Recommend recommend, Model model) {
		if(recommend.getSchool()!=null&&!StringUtils.isEmpty(recommend.getSchool().getId())){
			recommend.setSchool(schoolService.get(recommend.getSchool().getId()));
		}
		if(StringUtils.isEmpty(recommend.getId())&&!StringUtils.isEmpty(recommend.getProfession())){
			recommend.setProfession(schoolService.getProfIntro(recommend.getProfession()).getProfName());
		}

		model.addAttribute("recommend", recommend);
		return "yy/liuxue/recommendForm";
	}

	@RequiresPermissions("liuxue:recommend:edit")
	@RequestMapping(value = "save")
	public String save(Recommend recommend, Model model, RedirectAttributes redirectAttributes) {
		if(recommend.getUser()==null)
			recommend.setUser(UserUtils.getUser());
		if (!beanValidator(model, recommend)){
			return form(recommend, model);
		}
		recommendService.save(recommend);
		addMessage(redirectAttributes, "保存推荐成功");
		return "redirect:"+Global.getAdminPath()+"/liuxue/recommend/school";
	}
	
	@RequiresPermissions("liuxue:recommend:edit")
	@RequestMapping(value = "delete")
	public String delete(Recommend recommend, RedirectAttributes redirectAttributes) {
		recommendService.delete(recommend);
		addMessage(redirectAttributes, "删除推荐成功");
		return "redirect:"+Global.getAdminPath()+"/liuxue/recommend/school";
	}
	@RequiresPermissions("liuxue:recommend:view")
	@RequestMapping(value = {"studentlist"})
	public String studentList(Recommend recommend, HttpServletRequest request, HttpServletResponse response, Model model) {
		recommend.setStudent(UserUtils.getUser());
		Page<Recommend> page = recommendService.findPage(new Page<Recommend>(request, response), recommend); 
		model.addAttribute("page", page);
		return "yy/liuxue/recommendListStudent";
	}
	
	@RequiresPermissions("liuxue:recommend:edit")
	@RequestMapping(value = "school")
	public String school(School school,HttpServletRequest request, HttpServletResponse response, Model model) {
		
		Page<School> page = schoolService.findPage(new Page<School>(request, response), school); 
		model.addAttribute("page", page);
		return "yy/liuxue/schoolListRecommend";
	}
}